package com.client.ryt.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2014/7/11.
 */
public class DateUtil {
    public static String long2Date(long time,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date(time);
        return sdf.format(date);
    }



    public static String long2Date(long time){
        return long2Date(time,"yyyy-MM-dd");
    }

    public static String long2Time(long time,String format){
        return "";
    }
    public static String long2Time(long time){
        return long2Time(time,"");
    }

    /**
     * 几年几月几天前
     * @param timeMillis 毫秒
     * @return
     */
    public static String getShortTime(long timeMillis){

        return getShortTime(new Date(timeMillis));
    }

   /* public static String getShortTime(Date date) {
        String shortstring = null;
        if (date == null) return shortstring;
        long deltime = (System.currentTimeMillis() - date.getTime()) / 1000;
        if (deltime > 365 * 24 * 60 * 60) {
            shortstring = (int) (deltime / (365 * 24 * 60 * 60)) + "年前";
            return shortstring;
        }else if(deltime > 30 * 24 * 60 * 60){
            shortstring = (int) (deltime / (30 * 24 * 60 * 60)) + "月前";
            return shortstring;
        }else if (deltime > 24 * 60 * 60) {
            shortstring = (int) (deltime / (24 * 60 * 60)) + "天前";
            return shortstring;
        } else if (deltime > 60 * 60) {
            shortstring = (int) (deltime / (60 * 60)) + "小时前";
            return shortstring;
        } else if (deltime > 60) {
            shortstring = (int) (deltime / (60)) + "分钟前";
            return shortstring;
        } else if (deltime > 1) {
           // shortstring = deltime + "秒前";
            return "刚刚";
        } else {
            shortstring = "刚刚";
            return shortstring;
        }
    }*/

    public static String getShortTime(Date date){
        String weeHoursSDF = "凌晨 HH:mm";
        String morningSDF = "上午 HH:mm";
        String afternoonSDF = "下午 HH:mm";
        String nigthSDF = "晚上 HH:mm";
        String yesSDF = "昨天 HH:mm";
        String weekSDF = "HH:mm";
        String otherSDF = "MM月dd日";
        SimpleDateFormat sdf = null;
        String time = "";
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        Date now = new Date();

        Calendar targetCalendar_from = Calendar.getInstance();
        Calendar targetCalendar_to = Calendar.getInstance();
        targetCalendar_from.setTime(now);
        targetCalendar_to.setTime(now);

        targetCalendar_from.set(Calendar.HOUR_OF_DAY, 0);
        targetCalendar_from.set(Calendar.MINUTE, 0);
        targetCalendar_to.set(Calendar.HOUR_OF_DAY, 5);
        targetCalendar_to.set(Calendar.MINUTE, 59);
        if(dateCalendar.after(targetCalendar_from) && dateCalendar.before(targetCalendar_to)){
            sdf = new SimpleDateFormat(weeHoursSDF);
            return sdf.format(date);
        }

        targetCalendar_from.set(Calendar.HOUR_OF_DAY, 6);
        targetCalendar_from.set(Calendar.MINUTE, 0);
        targetCalendar_to.set(Calendar.HOUR_OF_DAY, 11);
        targetCalendar_to.set(Calendar.MINUTE, 59);
        if(dateCalendar.after(targetCalendar_from) && dateCalendar.before(targetCalendar_to)){
            sdf = new SimpleDateFormat(morningSDF);
            return sdf.format(date);
        }

        targetCalendar_from.set(Calendar.HOUR_OF_DAY, 12);
        targetCalendar_from.set(Calendar.MINUTE, 0);
        targetCalendar_to.set(Calendar.HOUR_OF_DAY, 17);
        targetCalendar_to.set(Calendar.MINUTE, 59);
        if(dateCalendar.after(targetCalendar_from) && dateCalendar.before(targetCalendar_to)){
            sdf = new SimpleDateFormat(afternoonSDF);
            return sdf.format(date);
        }

        targetCalendar_from.set(Calendar.HOUR_OF_DAY, 18);
        targetCalendar_from.set(Calendar.MINUTE, 0);
        targetCalendar_to.set(Calendar.HOUR_OF_DAY, 23);
        targetCalendar_to.set(Calendar.MINUTE, 59);
        if(dateCalendar.after(targetCalendar_from) && dateCalendar.before(targetCalendar_to)){
            sdf = new SimpleDateFormat(nigthSDF);
            return sdf.format(date);
        }

        //昨天
        targetCalendar_from.setTime(now);
        targetCalendar_to.setTime(now);
        targetCalendar_from.add(Calendar.DATE, -1);
        targetCalendar_to.add(Calendar.DATE,-1);
        targetCalendar_from.set(Calendar.HOUR_OF_DAY, 0);
        targetCalendar_from.set(Calendar.MINUTE, 0);
        targetCalendar_to.set(Calendar.HOUR_OF_DAY, 23);
        targetCalendar_to.set(Calendar.MINUTE, 59);
        if(dateCalendar.after(targetCalendar_from) && dateCalendar.before(targetCalendar_to)){
            sdf = new SimpleDateFormat(yesSDF);
            return sdf.format(date);
        }

        //2-5天
        targetCalendar_from.setTime(now);
        targetCalendar_to.setTime(now);
        targetCalendar_from.add(Calendar.DATE, -5);
        targetCalendar_to.add(Calendar.DATE,-2);
        targetCalendar_from.set(Calendar.HOUR_OF_DAY, 0);
        targetCalendar_from.set(Calendar.MINUTE, 0);
        targetCalendar_to.set(Calendar.HOUR_OF_DAY, 23);
        targetCalendar_to.set(Calendar.MINUTE, 59);
        if(dateCalendar.after(targetCalendar_from) && dateCalendar.before(targetCalendar_to)){
            String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
            int w = dateCalendar.get(Calendar.DAY_OF_WEEK) - 1;
            if (w < 0)
                w = 0;
            sdf = new SimpleDateFormat(weekSDF);
            return weekDays[w]+" "+ sdf.format(date);
        }

        //6天以上
        targetCalendar_to.setTime(now);
        targetCalendar_to.add(Calendar.DATE,-6);
        targetCalendar_to.set(Calendar.HOUR_OF_DAY, 23);
        targetCalendar_to.set(Calendar.MINUTE, 59);
        if(dateCalendar.before(targetCalendar_to)){
            sdf = new SimpleDateFormat(otherSDF);
            return sdf.format(date);
        }

        return "";
    }


    public static String getDate() {
        Calendar c = Calendar.getInstance();
        String year = String.valueOf(c.get(Calendar.YEAR));
        String month = String.valueOf(c.get(Calendar.MONTH));
        String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);
        String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
        String mins = String.valueOf(c.get(Calendar.MINUTE));

        StringBuffer sbBuffer = new StringBuffer();
        sbBuffer.append(year + "-" + month + "-" + day + " " + hour + ":"
                + mins);
        return sbBuffer.toString();
    }

}
